-- BonusBuilding
-- Created by cheny3 Dec/13/2016
-- 奖励建筑

-- 初始化
function initBonusBuilding(self)
    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    -- 建筑图标
    local iconPath = getLevelItemIconPath(buildingInfo["icon"]);
    local offsetY = tonumber(buildingInfo["icon_offset_y"]);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:setPositionY(self.icon:getPositionY() + offsetY);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    if self.gridData.need_shining == 1 then
        -- 闪光
        self.shiningAjustPos = cc.p(70, 100);
        shiningEffect(self.icon, self.shiningAjustPos.x, self.shiningAjustPos.y);
    end

    if self.gridData.need_animation == 1 then
        -- 呼吸
        self:doModelBreath(self.icon);
    end

    local function okFunc()
        local args = table.deepcopy(DungeonM.queryElement(self.gridData.element, "args"));
        local cost = args["cost"];

        local prefix = "formula_";
        if type(cost) == "string" and string.startWith(cost, prefix) then
            -- 配置了公式，先自行计算一遍
            local formulaName = string.sub(cost, string.len(prefix) + 1);
            cost = FormulaM.invoke(formulaName, ME.user);
        end

        if cost then
            if type(cost[1]) == "number" then
                cost = {cost, };
            end

            for _, perCost in pairs(cost) do
                if perCost[1] == 1 then
                    if ItemM.getAmount(ME.user, perCost[2]) < perCost[3] then
                        alert(string.format(getLocStr("something_not_enough"), ItemM.query(perCost[2],"name")));
                        return;
                    end
                elseif perCost[1] == 2 then
                    if ME.user:queryAttrib(perCost[2]) < perCost[3] then
                        alert(string.format(getLocStr("something_not_enough"), FieldsM.query(perCost[2],"name")));
                        return;
                    end
                end
            end
        end

        local pos = self.gridData:getPos();
        local remainGrid = self.gridData.remain_grid;

        -- 奖励
        DungeonActionM.go("do_building_bonus", pos, nil, {["remainGrid"] = remainGrid});

        if self.gridData.need_shining then
            self.icon:removeAllChildren();
        end
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                -- 没消失的话
                if self.gridData.remain_grid then
                    self:openBuildingView(nil, nil, buildingInfo["dialog2"], getLocStr("btn_text_leave"));
                end
                return;
            end

            -- 打开建筑查看界面
            if type(buildingInfo["view_text2"]) == "string" and string.len(buildingInfo["view_text2"]) > 0 then
                self:openBuildingView2(okFunc, nil, buildingInfo["view_text"], buildingInfo["view_text2"]);
            else
                self:openBuildingView(okFunc);
            end
        end
    end
    self.icon:addTouchEventListener(onClicked);
end
